﻿<%@ WebHandler Language="C#" Class="GetDataGrid" %>

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Text;
using System.Data;
using VBDWebMobile;

public class GetDataGrid : IHttpHandler, IReadOnlySessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        if (context.Session["User"] != null)
        {
            int nPage = Convert.ToInt32(context.Request.QueryString["page"]);
            int nRows = Convert.ToInt32(context.Request.QueryString["rows"]);
            int nToltal = 0;
            StringBuilder sData = new StringBuilder();
            string sTable = Newtonsoft.Json.JsonConvert.SerializeObject(CreateData(ref nToltal, (nPage - 1) * nRows, nRows));
            int nPageSize = VBDWebMobile.Untilities.PageNumber(nToltal, nRows);
            sData.Append("{");
            sData.Append("page:");
            sData.Append(nPage);
            sData.Append(",");
            sData.Append("total:");
            sData.Append(nPageSize);
            sData.Append(",");
            sData.Append("records:");
            sData.Append(nToltal);
            sData.Append(",");
            sData.Append("rows:");
            sData.Append(sTable);
            sData.Append("}");
            context.Response.Write(Newtonsoft.Json.JsonConvert.DeserializeObject(sData.ToString()));
        }
        //else
        //{
        //    context.Response.Write("");
        //}

    }
    private DataTable CreateData(ref int nTotal, int nStartRecords, int nRecords)
    {
        try
        {
            System.Collections.Generic.List<DeviceInfo> listDevice = new List<DeviceInfo>();
            GetAddress gsdd = new GetAddress();
            //DateTime dtCur = DateTime.UtcNow;

            DeviceInfoRepository deviceresponse = new DeviceInfoRepository();
            listDevice = deviceresponse.Select(Convert.ToInt32(HttpContext.Current.Session["UserID"]));

            //string st = (DateTime.UtcNow - dtCur).TotalSeconds.ToString();

            nTotal = listDevice.Count;
            var result = (from p in listDevice.Skip(nStartRecords).Take(nRecords)
                          select p).ToList();
            DataTable tb = CreateTable();
            string szLocationHref = "";
            string sAddress = "";

            foreach (var info in result)
            {
                //sAddress = Untilities.GetLocationInfo(info.Longitude, info.Latitude);
                sAddress = gsdd.GetGeoInfoByPoint(info.Longitude.ToString(), info.Latitude.ToString(), 0);
                switch (info.Status)
                {
                    case 0:
                        {
                            szLocationHref = String.Format("<a class=Address data-rel=dialog href=Map.aspx?lat={0}&lon={1}&level={2}&hw=", info.Latitude, info.Longitude, 15) + ">" + sAddress + "</a>";
                            break;
                        }
                    case 1:
                        {
                            szLocationHref = String.Format("<a class=AddressYellow data-rel=dialog href=Map.aspx?lat={0}&lon={1}&level={2}&hw=", info.Latitude, info.Longitude, 15) + ">" + sAddress + "</a>";
                            break;
                        }
                    case 2:
                        {
                            szLocationHref = String.Format("<a class=AddressRed data-rel=dialog href=Map.aspx?lat={0}&lon={1}&level={2}&hw=", info.Latitude, info.Longitude, 15) + ">" + sAddress + "</a>";
                            break;
                        }
                    case 3:
                        {
                            szLocationHref = String.Format("<a class=AddressGreen data-rel=dialog href=Map.aspx?lat={0}&lon={1}&level={2}&hw=", info.Latitude, info.Longitude, 15) + ">" + sAddress + "</a>";
                            break;
                        }
                }

                tb.Rows.Add(info.ID, info.Plate.Replace("_"," ").Replace("-"," "), String.Format("{0:0.00}",info.Speed), info.Status, info.Time, szLocationHref);
            }

            return tb;
        }
        catch (Exception)
        {

            return null;
        }

    }
    private static DataTable CreateTable()
    {
        DataTable tb = new DataTable();
        tb.Columns.Add("ID", typeof(long));
        tb.Columns.Add("Plate", typeof(string));
        tb.Columns.Add("Speed", typeof(float));
        tb.Columns.Add("Status", typeof(int));
        tb.Columns.Add("Time", typeof(string));
        tb.Columns.Add("Location", typeof(string));
        tb.Dispose();
        return tb;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }



}